package indi.zhifa.core.dataauth.handler;

import indi.zhifa.core.auth.interfaces.IAuthObject;
import indi.zhifa.core.auth.util.ITokenUtil;
import indi.zhifa.core.mpenhance.handler.BaseMetaObjectHandler;
import indi.zhifa.core.mpenhance.properties.MetaObjectHandlerProperty;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

public abstract class BaseAuthMetaObjectHandler extends BaseMetaObjectHandler {

    protected ITokenUtil mTokenUtil;

    @Override
    protected void insertCreateBy(MetaObject pMetaObject) {
        IAuthObject authObject = mTokenUtil.getAuthObject();
        this.strictInsertFill(pMetaObject,createByStr,()->authObject.getUserId(),Long.class);
    }

    @Override
    protected void insertCreateByName(MetaObject pMetaObject) {
        IAuthObject authObject = mTokenUtil.getAuthObject();
        this.strictInsertFill(pMetaObject,createByNameStr,()->authObject.getUserName(),String.class);
    }


    @Override
    protected boolean hasTokenObject() {
        if(null == mTokenUtil){
            mTokenUtil = mApplicationContext.getBean(ITokenUtil.class);
        }
        return mTokenUtil.hasTokenObject();
    }

    @Override
    protected void updateModifyBy(MetaObject pMetaObject) {
        IAuthObject authObject = mTokenUtil.getAuthObject();
        this.strictUpdateFill(pMetaObject,modifyByStr,()->authObject.getUserId(),Long.class);
    }

    @Override
    protected void updateModifyByName(MetaObject pMetaObject) {
        IAuthObject authObject = mTokenUtil.getAuthObject();
        this.strictUpdateFill(pMetaObject,modifyByNameStr,()->authObject.getUserName(),String.class);
    }

}
